package com.sure0000;

import com.jcraft.jsch.*;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @program: data_center
 * @description: java ssh 工具类
 * 使用说明：首先通过 getConnect 获取 session 通道，再去调用 ftp 或 cmd 功能，
 * 每个功能模块调用后应当调用其对应的 close 方法来关闭资源。
 * @author: xuyouchang@supcon.com
 * @create: 2019-11-07 19:21
 **/
public class SSHUtil {

    private Session session;

    private String username;

    private String password;

    private String host;

    private ChannelSftp channelSftp;

    private ChannelExec channelExec;

    private InputStream inputStream;

    private static final Integer TIME_OUT = 5000;

    private static final String ENCODING = "UTF-8";

    private static final Logger logger = LoggerFactory.getLogger(SSHUtil.class);

    public SSHUtil(String host, String username, String password)  {
        this.host = host;
        this.username = username;
        this.password = password;
    }

    // 设置连接信息
    public void getConnect() throws JSchException {
        JSch jsch = new JSch();
        session = jsch.getSession(username, host);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect(TIME_OUT);
    }

    // ssh 命令通道
    public String exeCommand(String command) throws JSchException, IOException {
        channelExec = (ChannelExec) session.openChannel("exec");
        inputStream = channelExec.getInputStream();
        channelExec.setCommand(command);
        channelExec.setErrStream(System.err);
        channelExec.connect();
        return IOUtils.toString(inputStream, ENCODING);
    }

    public void closeExeCommand() {
        try {
            inputStream.close();
        } catch (IOException e) {
            logger.warn("exe command inputStream close fail", e);
        }
        channelExec.disconnect();
        session.disconnect();
    }

    // ftp 上传文件
    public void ftpUpload(String sourceFilePath, String dstFilePath) throws JSchException, SftpException {
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();
        channelSftp.put(sourceFilePath, dstFilePath, ChannelSftp.OVERWRITE);
    }

    public InputStream ftpRead(String dstFilePath) throws JSchException, SftpException {
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();
        return channelSftp.get(dstFilePath);
    }

    public void closeFtp() {
        channelSftp.disconnect();
        session.disconnect();
    }

    public static void main(String[] args) throws JSchException, SftpException {
        SSHUtil sshUtil = new SSHUtil("10.10.77.138", "root", "supconit");
        sshUtil.getConnect();
        InputStream in = sshUtil.ftpRead("/usr/local/src/prometheus-2.13.1.linux-amd64/prometheus.yml");
        Yaml yaml = new Yaml();
        Map map = yaml.loadAs(in, Map.class);

    }

}
